package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcSupplierExpense;
import com.yxinsur.imc.business.enums.IsDeletedEnum;
import com.yxinsur.imc.business.form.SupplierExpenseForm;
import com.yxinsur.imc.business.mapper.ImcContractMapper;
import com.yxinsur.imc.business.mapper.ImcSupplierExpenseMapper;
import com.yxinsur.imc.business.service.IImcSupplierExpenseService;
import com.yxinsur.imc.business.vo.ImcSupplierExpenseVo;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 供应商-费用科目Service业务层处理
 *
 * @author imc
 * @date 2021-07-29
 */
@Service
public class ImcSupplierExpenseServiceImpl extends ServiceImpl<ImcSupplierExpenseMapper, ImcSupplierExpense> implements IImcSupplierExpenseService {
    @Autowired
    private ImcContractMapper imcContractMapper;
    @Autowired
    private ImcSupplierExpenseMapper imcSupplierExpenseMapper;

    /**
     * 供应商费用科目-查询列表
     *
     * @param supplierExpenseForm 供应商-费用科目
     * @return 供应商-费用科目
     */
    @Override
    public List<ImcSupplierExpenseVo> selectImcSupplierExpenseList(SupplierExpenseForm supplierExpenseForm) {
        ImcSupplierExpense supplierExpense = new ImcSupplierExpense();
        BeanUtil.copyProperties(supplierExpenseForm, supplierExpense);
        supplierExpense.setTenantId(SecurityUtils.getTenantId());
        if (ObjectUtil.isEmpty(supplierExpenseForm.getRepoSupplierId()) && ObjectUtil.isNotEmpty(supplierExpenseForm.getContractId())) {
            supplierExpense.setRepoSupplierId(imcContractMapper.getRepoSupplierIdById(supplierExpenseForm.getContractId()));
        }
        return this.imcSupplierExpenseMapper.selectImcSupplierExpenseList(supplierExpense);
    }

    /**
     * 供应商费用科目-查询详请
     *
     * @param id 供应商-费用科目id
     * @return 供应商-费用科目
     */
    @Override
    public ImcSupplierExpenseVo selectImcSupplierExpenseById(Long id) {
        ImcSupplierExpenseVo imcSupplierExpenseVo = new ImcSupplierExpenseVo();
        ImcSupplierExpense imcSupplierExpense = imcSupplierExpenseMapper.getImcSupplierExpenseById(id);
        BeanUtil.copyProperties(imcSupplierExpense, imcSupplierExpenseVo);
        return imcSupplierExpenseVo;
    }


    /**
     * 供应商费用科目-新增
     * <p>
     * 1.同一个租户同一个供应商只能有一个同名费用科目
     * </p>
     *
     * @param supplierExpenseForm 供应商-费用科目
     * @return 结果
     */
    @Override
    public AjaxResult insertImcSupplierExpense(SupplierExpenseForm supplierExpenseForm) {
        // 校验：同一供应商不能配置相同的费用科目
        ImcSupplierExpense existImcSupplierExpense = this.imcSupplierExpenseMapper.getImcSupplierExpenseByRepoSupplierIdAndExpenseName(supplierExpenseForm.getRepoSupplierId(), supplierExpenseForm.getExpenseName(), SecurityUtils.getTenantId());
        if (ObjectUtil.isNotEmpty(existImcSupplierExpense)) {
            return AjaxResult.error("该供应商已存在该费用科目，不可重复添加");
        }
        // 保存：费用科目
        ImcSupplierExpense supplierExpense = new ImcSupplierExpense();
        BeanUtil.copyProperties(supplierExpenseForm, supplierExpense);
        supplierExpense.setCreateId(SecurityUtils.getUsername());
        supplierExpense.setUpdateId(SecurityUtils.getUsername());
        supplierExpense.setCreateTime(DateUtils.getNowDate());
        supplierExpense.setUpdateTime(DateUtils.getNowDate());
        supplierExpense.setIsDeleted(IsDeletedEnum.N.getCode());
        supplierExpense.setTenantId(SecurityUtils.getTenantId());
        this.imcSupplierExpenseMapper.insertImcSupplierExpense(supplierExpense);
        return AjaxResult.success();
    }

    /**
     * 修改
     *
     * @param supplierExpenseForm 供应商-费用科目
     * @return 结果
     */
    @Override
    public AjaxResult updateImcSupplierExpense(SupplierExpenseForm supplierExpenseForm) {

        ImcSupplierExpense imcSupplierExpense = this.imcSupplierExpenseMapper.getImcSupplierExpenseById(supplierExpenseForm.getId());
        if (ObjectUtil.isEmpty(imcSupplierExpense)) {
            return AjaxResult.error("系统内不存在该供应商费用科目，请核实后再操作");
        }
        // 校验：同一供应商不能配置相同的费用科目
        if (!imcSupplierExpense.getExpenseName().equals(supplierExpenseForm.getExpenseName())) {
            ImcSupplierExpense existImcSupplierExpense = this.imcSupplierExpenseMapper.getImcSupplierExpenseByRepoSupplierIdAndExpenseName(supplierExpenseForm.getRepoSupplierId(), supplierExpenseForm.getExpenseName(), SecurityUtils.getTenantId());
            if (ObjectUtil.isNotEmpty(existImcSupplierExpense)) {
                return AjaxResult.error("该供应商已存在该费用科目，不可重复添加");
            }
        }

        // 保存：费用科目
        BeanUtil.copyProperties(supplierExpenseForm, imcSupplierExpense, CopyOptions.create().setIgnoreCase(true));
        imcSupplierExpense.setUpdateId(SecurityUtils.getUsername());
        imcSupplierExpense.setUpdateTime(DateUtils.getNowDate());
        imcSupplierExpenseMapper.updateImcSupplierExpense(imcSupplierExpense);
        return AjaxResult.success();
    }

    /**
     * 删除供应商-费用科目信息
     *
     * @param id 供应商-费用科目ID
     * @return 结果
     */
    @Override
    public int deleteImcSupplierExpenseById(String id) {
        return imcSupplierExpenseMapper.deleteImcSupplierExpenseById(id);
    }
}
